/**
 * Created by JetBrains WebStorm.
 * User: nhuang
 * Date: 12-4-10
 * Time: 下午5:10
 * To change this template use File | Settings | File Templates.
 */
(function ($) {
    $.fn.nTip = function (options) {
        var defaults = {
            tipColor:"#cccccc",
            tipBackground:"#F6FCE2",
            tipBorder:"#159CE9",
            animateShow:"fadeIn",
            animateHide:"fadeOut",
            delayTime:500,
            showEvent:"mouseover",
            pointyPosition:5,
            vertical:false,
            verTipBox:"verTip",
            verPoinShadow:"verTipShadow",
            verPointy:"verTipPointy",
            norTipBox:"norTip",
            norPoinShadow:"norTipShadow",
            norPointy:"norTipPointy"
        };
        var opts = $.extend(defaults, options);

        return this.each(function () {
            var thisTip = $(this);
            if (!thisTip.attr('text')) return true;
            var TipShadow, TipPointy, TipBox, pointy, colorPointyShadow, colorPointy;

            function createTip() {
                if (opts.vertical) {
                    TipBox = opts.verTipBox;
                    TipShadow = opts.verPoinShadow;
                    TipPointy = opts.verPointy;
                    pointy = "top";
                    colorPointyShadow = "transparent " + opts.tipBorder + " transparent transparent ";
                    colorPointy = "transparent " + opts.tipBackground + " transparent transparent ";
                }
                else {
                    TipBox = opts.norTipBox;
                    TipShadow = opts.norPoinShadow;
                    TipPointy = opts.norPointy;
                    pointy = "left";
                    colorPointyShadow = opts.tipBorder + " transparent transparent transparent";
                    colorPointy = opts.tipBackground + " transparent transparent transparent"
                }
                thisTip.after('<span class="nTip ' + TipBox + '" style="background:' + opts.tipBackground + ';border-color:' + opts.tipBorder + ' ;color:' + opts.tipColor + ' ">' + thisTip.attr("text") + '<span class="pointyTipShadow ' + TipShadow + '" style="border-color:' + colorPointyShadow + ';' + pointy + ':' + opts.pointyPosition + 'px"></span><span class="pointyTip ' + TipPointy + '" style="border-color:' + colorPointy + ';' + pointy + ':' + opts.pointyPosition + 'px"></span></span>');
                $(".nTip")[opts.animateShow](opts.delayTime)
            }

            thisTip.bind(opts.showEvent, function () {
                createTip()
            });
            thisTip.parent().bind("mouseleave", function () {
                $(this).find(".nTip")[opts.animateHide](opts.delayTime,function(){
                    $(this).remove()
                });
            })
        })
    }
})(jQuery);